package com.mantru.iotpot.Dialog;

import static android.content.Context.INPUT_METHOD_SERVICE;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.mantru.iotpot.ApplicationClass;
import com.mantru.iotpot.R;
import com.mantru.iotpot.Util.ToastUtils;

public class ManufacturerDialog extends Dialog {

    Activity context;
    private TextView confirm;
    private EditText editText;
    private ImageView close;
    private ApplicationClass ac;
    private ManufacturerDialogListener manufacturerDialogListener;
    public void setManufacturerDialogListener(ManufacturerDialogListener manufacturerDialogListener){
        this.manufacturerDialogListener = manufacturerDialogListener;
    }
    public interface ManufacturerDialogListener{
        void confirm();
    }

    public ManufacturerDialog(Activity context, int theme){
        super(context, theme);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.dialog_manufacturer);
        ac = ((ApplicationClass)context.getApplication());
        initView();//set my control here
        setDialogCancelable();//set dialog cancelable or not
        setDialogListener();
    }

    //set my control here
    private void initView(){
        editText = findViewById(R.id.dialog_manufacturer_edittext);
        confirm = findViewById(R.id.dialog_manufacturer_confirm);
        close = findViewById(R.id.dialog_manufacturer_close);
    }

    private void setDialogCancelable(){
        this.setCancelable(true);
    }

    private void setDialogListener(){
        editText.setOnEditorActionListener((v, actionId, event) -> {
            // 检查是否是按下 Enter 键或者完成操作
            if ((event != null && (event.getAction() == KeyEvent.ACTION_DOWN) && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER))
                    || actionId == EditorInfo.IME_ACTION_DONE
                    || actionId == EditorInfo.IME_ACTION_NEXT
                    || actionId == EditorInfo.IME_ACTION_SEND) {
                // 获取输入法管理器
                InputMethodManager imm = (InputMethodManager) context.getSystemService(INPUT_METHOD_SERVICE);
                // 隐藏软键盘
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
                if ( !editText.getText().toString().trim().equals(ac.admin_password) ){
                    ToastUtils.show(context, "密码错误，请重新输入");
                    return true;
                }
                manufacturerDialogListener.confirm();
                dismiss();
                return true;
            }
            return false;
        });

        confirm.setOnClickListener(v -> {
            if ( !editText.getText().toString().trim().equals(ac.admin_password) ){
                ToastUtils.show(context, "密码错误，请重新输入");
                return;
            }
            manufacturerDialogListener.confirm();
            dismiss();
        });

        close.setOnClickListener(v -> dismiss());
    }
}
